package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import gs.*;
	import gs.easing.*;
	import gs.events.TweenEvent;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.tree.AbstractLayoutCache;
	public class FrameTalk extends JFrame
	{
		//members define
		private var panelTop:JPanel;
		private var panelBottom:JPanel;
		private var panelImageSelf:JPanel;
		private var labelNameSelf:JLabel;
		private var panelImageOther:JPanel;
		private var labelNameOther:JLabel;
		private var panelTalkSelf:JPanel;
		private var txtTalkSelf:JTextArea;
		private var panelTalkOther:JPanel;
		private var txtTalkOther:JTextArea;
		
		public function FrameTalk()
		{
			setClosable(false);
			setResizable(false);  
			setDragable(false);
			setBorder(new EmptyBorder());
			this.setTitleBar(null);
			this.setBackgroundDecorator(null);
			
			//component creation
			setSize(new IntDimension(1200, 600));
			setPreferredSize(new IntDimension(1200, 600));
			var layout0:EmptyLayout = new EmptyLayout();
			getContentPane().setLayout(layout0);
			
			panelTop = new JPanel();
			panelTop.setLocation(new IntPoint(0, 0));
			panelTop.setSize(new IntDimension(1200, 80));
			panelTop.setPreferredSize(new IntDimension(1200, 80));
			
			panelBottom = new JPanel();
			panelBottom.setLocation(new IntPoint(0, 480));
			panelBottom.setSize(new IntDimension(1200, 120));
			panelBottom.setPreferredSize(new IntDimension(1200, 120));
			var layout1:EmptyLayout = new EmptyLayout();
			panelBottom.setLayout(layout1);
			
			panelImageSelf = new JPanel();
			panelImageSelf.setLocation(new IntPoint(0, 0));
			panelImageSelf.setSize(new IntDimension(200, 100));
			panelImageSelf.setPreferredSize(new IntDimension(200, 100));
			
			labelNameSelf = new JLabel();
			labelNameSelf.setLocation(new IntPoint(0, 100));
			labelNameSelf.setSize(new IntDimension(200, 20));
			labelNameSelf.setPreferredSize(new IntDimension(200, 20));
			labelNameSelf.setText("label");
			
			panelImageOther = new JPanel();
			panelImageOther.setLocation(new IntPoint(1000, 0));
			panelImageOther.setSize(new IntDimension(200, 100));
			panelImageOther.setPreferredSize(new IntDimension(200, 100));
			
			labelNameOther = new JLabel();
			labelNameOther.setLocation(new IntPoint(1000, 100));
			labelNameOther.setSize(new IntDimension(200, 20));
			labelNameOther.setPreferredSize(new IntDimension(200, 20));
			labelNameOther.setText("label");
			
			panelTalkSelf = new JPanel();
			panelTalkSelf.setLocation(new IntPoint(200, 0));
			panelTalkSelf.setSize(new IntDimension(600, 120));
			panelTalkSelf.setPreferredSize(new IntDimension(600, 120));
			var layout2:CenterLayout = new CenterLayout();
			panelTalkSelf.setLayout(layout2);
			
			txtTalkSelf = new JTextArea();
			txtTalkSelf.setLocation(new IntPoint(144, 49));
			txtTalkSelf.setSize(new IntDimension(12, 21));
			
			panelTalkOther = new JPanel();
			panelTalkOther.setLocation(new IntPoint(400, 0));
			panelTalkOther.setSize(new IntDimension(600, 120));
			panelTalkOther.setPreferredSize(new IntDimension(600, 120));
			var layout3:CenterLayout = new CenterLayout();
			panelTalkOther.setLayout(layout3);
			
			txtTalkOther = new JTextArea();
			txtTalkOther.setLocation(new IntPoint(580, 0));
			txtTalkOther.setSize(new IntDimension(12, 21));
			
			//component layoution
			getContentPane().append(panelTop);
			getContentPane().append(panelBottom);
			
			panelBottom.append(panelImageSelf);
			panelBottom.append(labelNameSelf);
			panelBottom.append(panelImageOther);
			panelBottom.append(labelNameOther);
			panelBottom.append(panelTalkSelf);
			panelBottom.append(panelTalkOther);
			
			panelTalkSelf.append(txtTalkSelf);
			
			panelTalkOther.append(txtTalkOther);
			
			//
			
			panelTop.setBackgroundDecorator(new SolidBackground(new ASColor(0x000000,0.9)));
			panelBottom.setBackgroundDecorator(new SolidBackground(new ASColor(0x000000,0.9)));
			
			txtTalkSelf.setEditable(false);
			txtTalkSelf.setWordWrap(true);
			txtTalkSelf.setBackgroundDecorator(null);
			
			txtTalkOther.setEditable(false);
			txtTalkOther.setWordWrap(true);
			txtTalkOther.setBackgroundDecorator(null);
			
			addEventListener(MouseEvent.CLICK, onClick);
			
			txtTalkSelf.setFont(new ASFont("宋体", 14, false, false, false, false));
			txtTalkSelf.filters = new Array(new GlowFilter(0x000000, 1, 2, 2, 10));
			txtTalkSelf.setForeground(ASColor.WHITE);
			
			txtTalkOther.setFont(new ASFont("宋体", 14, false, false, false, false));
			txtTalkOther.filters = new Array(new GlowFilter(0x000000, 1, 2, 2, 10));
			txtTalkOther.setForeground(ASColor.WHITE);
			
			txtTalkSelf.setPreferredWidth(500);
			txtTalkOther.setPreferredWidth(500);
			
			labelNameSelf.setFont(new ASFont("宋体", 14, true, false, false, false));
			labelNameSelf.filters = new Array(new GlowFilter(0x000000, 1, 2, 2, 10));
			labelNameSelf.setForeground(ASColor.ORANGE);
			
			labelNameOther.setFont(new ASFont("宋体", 14, true, false, false, false));
			labelNameOther.filters = new Array(new GlowFilter(0x000000, 1, 2, 2, 10));
			labelNameOther.setForeground(ASColor.ORANGE);
			
			this.setClipMasked(false);
			panelImageSelf.setClipMasked(false);
			panelImageOther.setClipMasked(false);
			panelBottom.setClipMasked(false);
		}
		private function onClick(e:Event):void
		{
			if(isLeaving)
				return;
			if(_contextCountSelf < _strContentSelf.length || _contextCountOther < _strContentOther.length)
			{
				_contextCountSelf = _strContentSelf.length;
				_contextCountOther = _strContentOther.length;
				if(_strContentSelf.length != 0)
					txtTalkSelf.setHtmlText(_strContentSelf);
				if(_strContentOther.length != 0)
					txtTalkOther.setHtmlText(_strContentOther);
				_timerMove.stop();
				return;
			}
			
			Client.getInstance().sendPacketLogic(902,[int(_nNPCDialogNpcId),int(_nStep)]);
			if(_nStep >= _nStepSize-1)
			{
				onLeave();
			}
		}
		private var _enter:Boolean = false;
		private function onEnter():void
		{
			if(_enter)
				return;
			isLeaving = false;
			panelTop.x = 0;
			panelTop.y = -80;
			TweenLite.to(panelTop,0.3,{x:0, y:0});
			
			panelBottom.x = 0;
			panelBottom.y = 600;
			TweenLite.to(panelBottom,0.3,{x:0, y:480,onComplete:function():void
			{
				PanelMainHelp.getInstance().onEnterTalk();
				_enter = true;
			}});
		}
		private var isLeaving:Boolean = false;
		private function onLeave():void
		{
			if(!_enter)
				return;
			panelTop.x = 0;
			panelTop.y = 0;
			TweenLite.to(panelTop,0.3,{x:0, y:-80});
			
			panelBottom.x = 0;
			panelBottom.y = 480;
			isLeaving = true;
			TweenLite.to(panelBottom,0.3,{x:0, y:600,onComplete:function():void
			{
				leave();
				isLeaving = false;
			}});
			
		}
		public function leave():void
		{
			if(!_enter)
				return;
			FrameTalk.hide();
			PanelMainHelp.getInstance().onLeaveTalk();
			_enter = false;
		}
		public static var _frame:FrameTalk = null;
		public static var _nNPCDialogNpcId:int = 0;
		public static var _nType:int = 0;
		public static var _nImageIdSelf:int = 0;
		public static var _strNameSelf:String = "";
		public static var _nImageIdOther:int = 0;
		public static var _strNameOther:String = "";
		public static var _nStep:int = 0;
		public static var _nStepSize:int = 0;
		public static var _strContentSelf:String = "";
		public static var _strContentOther:String = "";
		public static function show():void
		{
			//if(isShowing())
			//{
				//_frame.setData();
			//	return;
			//}
			if(_frame == null)
			{
				_frame = new FrameTalk();
				Client.getInstance().mainWindow.addChild(_frame);
			}
			_frame.show();
			//PanelMainHelp.getInstance().onEnterTalk();
			Client.getInstance().mainWindow.bringToTop(_frame);
			_frame.onEnter();
			_frame.setData();
			
		}
		public static function hide():void
		{
			//if(_frame._mcNPCImage != null && _frame.containsChild(_frame._mcNPCImage))
			//{
			//	_frame.removeChild(_frame._mcNPCImage);
			//	_frame._mcNPCImage = null;
			//}
			if(isShowing())
			{
				_frame.hide();
				//PanelMainHelp.getInstance().onLeaveTalk();
			}
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
		
		public static function phTalk_902(e:PacketEvent):void
		{
			if(_frame != null)
			{
				_frame.clearData();
			}
			
			var msg:Array = e.data as Array;
			trace("phTalk_902", msg);
			var nIndex:uint = 0;
			_nNPCDialogNpcId = msg[nIndex++];
			_nType = msg[nIndex++];
			
			if(_nType == 0)
			{
				_nImageIdSelf = msg[nIndex++];
				_strNameSelf = msg[nIndex++];
			}
			else
			{
				_nImageIdOther = msg[nIndex++];
				_strNameOther = msg[nIndex++];
			}
			
			_nStep = msg[nIndex++];
			_nStepSize = msg[nIndex++];
			
			if(_nType == 0)
				_strContentSelf = msg[nIndex++];
			else
				_strContentOther = msg[nIndex++];
			show();
		}
		private function onTimerMove(e:TimerEvent):void
		{
			
			_contextCountSelf++;
			_contextCountOther++;
			
			if(_contextCountSelf > _strContentSelf.length && _contextCountOther > _strContentOther.length)
			{
				_timerMove.stop();
				return;
			}
			if(_strContentSelf.length != 0)
				txtTalkSelf.setHtmlText(_strContentSelf.substr(0,_contextCountSelf > _strContentSelf.length ? _strContentSelf.length : _contextCountSelf));
			if(_strContentOther.length != 0)
				txtTalkOther.setHtmlText(_strContentOther.substr(0,_contextCountOther > _strContentOther.length ? _strContentOther.length : _contextCountOther));
			//labelContent.setHtmlText(_strContent.substr(0,_contextCount));
		}
		public function clearData():void
		{
			if(_mcImageSelf != null && panelImageSelf.contains(_mcImageSelf))
			{
				panelImageSelf.removeChild(_mcImageSelf)
				_mcImageSelf = null;
			}
			
			if(_mcImageOther != null && panelImageOther.contains(_mcImageOther))
			{
				panelImageOther.removeChild(_mcImageOther)
				_mcImageOther = null;
			}
			
			_contextCountSelf = 1;
			_contextCountOther = 1;
			_strContentSelf = "";
			_strContentOther = "";
			_strNameSelf = "";
			_strNameOther = "";
			labelNameSelf.setText("");
			labelNameOther.setText("");
			_nImageIdSelf = 0;
			_nImageIdOther = 0;
			txtTalkSelf.setHtmlText("");
			txtTalkOther.setHtmlText("");
		}
		private var _timerMove:Timer = null;
		private var _contextCountSelf:int = 1;
		private var _contextCountOther:int = 1;
		public function setData():void
		{
			_contextCountSelf = 1;
			_contextCountOther = 1;
			
			if(_mcImageSelf != null && panelImageSelf.contains(_mcImageSelf))
			{
				panelImageSelf.removeChild(_mcImageSelf)
				_mcImageSelf = null;
			}
			
			if(_mcImageOther != null && panelImageOther.contains(_mcImageOther))
			{
				panelImageOther.removeChild(_mcImageOther)
				_mcImageOther = null;
			}
			
			if(_timerMove == null)
			{
				_timerMove = new Timer(100);
				_timerMove.addEventListener(TimerEvent.TIMER,onTimerMove);
			}
			if(!_timerMove.running)
				_timerMove.start();
			
			//txtTalkSelf.setHtmlText('<font color=\"#d9941b\">'+_currentCharNpc.getName()+':</font>'+_strNPCDialogAsk);
			if(_strContentSelf.length != 0)
				txtTalkSelf.setHtmlText(_strContentSelf.substr(0,_contextCountSelf));
			if(_strContentOther.length != 0)
				txtTalkOther.setHtmlText(_strContentOther.substr(0,_contextCountOther));

			if(_nImageIdSelf != 0)
				LoaderManager.getInstance().load(Engine.assetMapNpcPath + "npc_"+ _nImageIdSelf + '.swf',onImageSelfLoaded);
			if(_nImageIdOther != 0)
				LoaderManager.getInstance().load(Engine.assetMapNpcPath + "npc_"+ _nImageIdOther + '.swf',onImageOtherLoaded);
			
			labelNameSelf.setText(_strNameSelf);
			labelNameOther.setText(_strNameOther);
			
			txtTalkSelf.pack();
			txtTalkOther.pack();
			this.pack();
			this.paintImmediately();
		}
		
		//private var _loader:LoaderData;
		private var _mcImageSelf:MovieClip = null;
		private var _mcImageOther:MovieClip = null;
		private function onImageSelfLoaded(e:Event):void
		{
			var l:LoaderInfo=e.target as LoaderInfo;
			l.removeEventListener(Event.COMPLETE,onImageSelfLoaded);
			//l.removeEventListener(IOErrorEvent.IO_ERROR,onLoadedError);
			var loader:LoaderData = l.loader as LoaderData;
			if(loader != null)
			{
				if(_mcImageSelf != null && panelImageSelf.contains(_mcImageSelf))
				{
					panelImageSelf.removeChild(_mcImageSelf)
					_mcImageSelf = null;
				}
				_mcImageSelf = loader.getElement("mc_object_stand",true) as MovieClip;//e.target.content as MovieClip;//
				if(_mcImageSelf == null)
					return;
				_mcImageSelf.cacheAsBitmap = true;
				_mcImageSelf.gotoAndStop(1);
				_mcImageSelf.scaleX = panelImageSelf.width/_mcImageSelf.width;
				_mcImageSelf.scaleY = _mcImageSelf.scaleX ;
				_mcImageSelf.x = (panelImageSelf.width - _mcImageSelf.width)/2;//+_mcPet.width/2;
				_mcImageSelf.y = panelImageSelf.height - _mcImageSelf.height;
				panelImageSelf.setBackgroundDecorator(null);
				panelImageSelf.addChildAt(_mcImageSelf,0);
			}
		}
		
		private function onImageOtherLoaded(e:Event):void
		{
			var l:LoaderInfo=e.target as LoaderInfo;
			l.removeEventListener(Event.COMPLETE,onImageOtherLoaded);
			//l.removeEventListener(IOErrorEvent.IO_ERROR,onLoadedError);
			var loader:LoaderData = l.loader as LoaderData;
			if(loader != null)
			{
				//var mcTmp:MovieClip = Engine.getElement(_appDomain,"mc_object") as MovieClip;
				if(_mcImageOther != null && panelImageOther.contains(_mcImageOther))
				{
					panelImageOther.removeChild(_mcImageOther)
					_mcImageOther = null;
				}
				_mcImageOther = loader.getElement("mc_object_stand",true) as MovieClip;//e.target.content as MovieClip;//
				if(_mcImageOther == null)
					return;
				_mcImageOther.cacheAsBitmap = true;
				_mcImageOther.gotoAndStop(1);
				_mcImageOther.scaleX = panelImageOther.width/_mcImageOther.width;//panelImageOther.height/_mcImageOther.height;
				_mcImageOther.scaleY = _mcImageOther.scaleX ;//panelImageOther.height/_mcImageOther.height;
				_mcImageOther.x = (panelImageOther.width - _mcImageOther.width)/2;//+_mcPet.width/2;
				_mcImageOther.y = panelImageOther.height - _mcImageOther.height;
				panelImageOther.setBackgroundDecorator(null);
				panelImageOther.addChildAt(_mcImageOther,0);
			}
		}
	}
}